import request from '@/config/axios'
import {config} from "@/config/axios/config";
import {getAccessToken} from "@/utils/auth";
import {useXStream} from "vue-element-plus-x";
import {EventSourceMessage, fetchEventSource} from "@microsoft/fetch-event-source";
import {listType} from "@/views/ai/agent/x6/content-renderer";
import type {BubbleListItemProps} from "vue-element-plus-x/types/components/BubbleList/types";

import {h} from "vue";
export interface BlueprintDo{
  id?:string,
  name?:string,
  remark?:string,
  code?:string,
  tags?:string[],
  cells?:any[]
}
export const BluePrintApi = {
  insert: async (v: BlueprintDo) => {
    return await request.post({ url: '/agent/blue-print/insert', data: v })
  },
  save: async (v: BlueprintDo) => {
    return await request.post({ url: '/agent/blue-print/save', data: v })
  },
  //sse 接口
  execute: async (
    id: string,
    input: any,
    onMessage: (ev: EventSourceMessage) => void,
    onClose: () => void,
    ctrl?,
    onError?
  ) => {
    const token = getAccessToken()
    return fetchEventSource(`${config.base_url}/agent/blue-print/execute/${id}`, {
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${token}`
      },
      openWhenHidden: true,
      body: JSON.stringify(input),
      onmessage: onMessage,
      onerror: onError,
      onclose: onClose,
      signal: ctrl?.signal
    })
  },
  executeByCode: async (
    id: string,
    input: any,
    onMessage: (ev: EventSourceMessage) => void,
    onClose: () => void,
    ctrl?,
    onError?
  ) => {
    const token = getAccessToken()
    return fetchEventSource(`${config.base_url}/agent/blue-print/executeByCode/${id}`, {
      method: 'post',
      headers: {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${token}`
      },
      openWhenHidden: true,
      body: JSON.stringify(input),
      onmessage: onMessage,
      onerror: onError,
      onclose: onClose,
      signal: ctrl?.signal
    })
  },
  page: async (query: any) => {
    return await request.get({ url: '/agent/blue-print/page', params: query })
  },
  info: async (id: string) => {
    return await request.get({ url: '/agent/blue-print/info', params: { id } })
  },
  del: async (id: string) => {
    return await request.get({ url: '/agent/blue-print/del', params: { id } })
  },
  //返回his-id
  createHis: async (flowId: string, chat: listType) => {
    return await request.post({
      url: '/agent/blue-print/create/chat-history',
      data: { flowId, ...chat }
    })
  },
  //批量保存聊天记录
  pushChat2His: async (hisId: string, chats: listType[]) => {
    return await request.post({ url: `/agent/blue-print/push/chat/${hisId}`, data: chats })
  },
  uploadFile2: async (url: string, platform: string) => {
    return await request.post({
      url: '/agent/blue-print/upload_file_2_ai_by_url',
      data: { url, platform }
    })
  },
  doc2Html: async (url: string) => {
    return await request.post({ url: '/agent/doc/2html', data: { url } })
  }
}
export type ChatType = BubbleListItemProps & {
  role: 'user' | 'ai' | 'system',
  chunk?:Chunk[],
  atTime?:Date,
  files?:string[],
  component?:string,
  think?:string,
  thinking?:boolean,
  props?:any,
}
export interface Chunk{
  node:string,
  content?:string,
  data?:any,
  status:string|'progressing'|'start'|'completed',
}
